<?php include('../../inc/init.php');?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6kbbsV7.1 &gt; 6kbbsV8.0</title>
<style>
</style>
<body>
<ul>
<li><a href="index.php?step=1&page=1&epagenum=5000">步骤1: 转换用户数据</a></li>
<li><a href="index.php?step=2&page=1&epagenum=5000">步骤2: 转换版块数据</a></li>
<li><a href="index.php?step=3&page=1&epagenum=5000">步骤3: 转换帖子标题数据</a></li>
<li><a href="index.php?step=4&page=1&epagenum=5000">步骤4: 转换帖子内容数据</a></li>
</ul>
<?php
$databasepath="old.mdb";
$dbusername="";
$dbpassword="";
include_once("access.php");
$access=new Access($databasepath,$dbusername,$dbpassword);
$step=intval($_GET['step']);
$page=intval($_GET['page']);
if($page<1){$page=1;}
$epagenum=intval($_GET['epagenum']);
$id_min=($page-1)*$epagenum+1;
$id_max=$page*$epagenum;
switch($step){
	case 1:
		/*******用户数据********/
		$page==1 && $db->row_delete("users","id>0");
		$maxrs=$access->first_array("select max(userid) as maxid from [user]");
		$maxid=intval($maxrs['maxid']);
		if($id_min>$maxid){
			$adminrss=$access->query("select * from [admin] where bd=0");
			while($access->fetch_row($adminrss)){
				$nnnadmin=addslashes(_iconv(odbc_result($adminrss,'name')));
				$nnuser['groupid']=GROUP_ADMIN;
				$db->row_update("users",$nnuser,"username='{$nnnadmin}'");
			}
			exit("用户数据转换完毕，请进行下一步骤。");
		}
		$users=array();
		$rs=$access->query("select * from [user] where userid BETWEEN {$id_min} AND {$id_max}");
		while($access->fetch_row($rs)){
			$userobj['id']=odbc_result($rs,'userid');
			$userobj['username']=addslashes(_iconv(odbc_result($rs,'name')));
			if(empty($userobj['username'])){$userobj['username']=$userobj['id'];}
			$userobj['userpass']=odbc_result($rs,'password');
			$userinfo=_iconv(odbc_result($rs,'userinfo'));
			$userinfo=explode("|",$userinfo);
			$userobj['email']=_iconv($userinfo[0]);
			$userobj['credits1']=odbc_result($rs,'mark');
			$userobj['postsnum']=odbc_result($rs,'alltopicnum');
			$userobj['groupid']= intval(odbc_result($rs,'grade'))==16? 203 : 1;
			$userobj['signuptime']=strtotime($userinfo[9])-8*3600;
			array_push($users,$userobj);
		}
		foreach($users as $key=>$user){
			$db->row_insert("users",$user);
			//var_dump($user);
			$user['username']=htmlFilter($user['username']);
			//echo"{$user['username']} 导入成功。<br />";
		}
		$page++;
		echo("<script>window.location.href='index.php?step={$step}&page={$page}&epagenum={$epagenum}';</script>");
	break;

	case 2:
		/*******版块数据********/
		$access->query("update [bdinfo] set id=id+1000 where id>0 and followid=0");
		$access->query("update [bdinfo] set followid=followid+1000 where id>0 and followid>0");

		$page==1 && $db->row_delete("forums","id>0");
		$forums=array();
		$rs=$access->query("select * from [bdinfo]");
		while($access->fetch_row($rs)){
			$forumobj['id']=odbc_result($rs,'id');
			$forumobj['pid']=odbc_result($rs,'followid');
			$bdinfo=_iconv(odbc_result($rs,'bdinfo'));
			$bdinfo=explode("|",$bdinfo);
			$forumobj['forumname']=addslashes($bdinfo[0]);
			$forumobj['forumcontent']=addslashes($bdinfo[1]);
			$forumobj['topicsnum']=odbc_result($rs,'topicnum');
			$forumobj['postsnum']=odbc_result($rs,'topicnum');

			array_push($forums,$forumobj);
		}
		foreach($forums as $key=>$forum){
			$db->row_insert("forums",$forum);
			//echo"{$forum['forumname']} 导入成功。<br />";
		}
		exit("版块数据转换完毕，请进行下一步骤。");
	break;
	
	case 3:
		//TODO:
		/*******帖子数据******特殊字符问题 html **/
		$page==1 && $db->row_delete("topics","id>0");
		$maxrs=$access->first_array("select max(id) as maxid from [topic]");
		$maxid=intval($maxrs['maxid']);
		if($id_min>$maxid){
			exit("帖子标题数据转换完毕");
		}
		$topics=array();
		$rs=$access->query("select * from [topic] where id BETWEEN {$id_min} AND {$id_max}");
		while($access->fetch_row($rs)){
			$topicinfo=_iconv(odbc_result($rs,'topicinfo'));
			$topicinfo=explode("|",$topicinfo);
			$retopic=_iconv(odbc_result($rs,'retopic'));
			$retopic=explode("|",$retopic);
			$istop=intval(odbc_result($rs,'top'))==1?1:0;
			$isalltop=intval(odbc_result($rs,'top'))==2?1:0;
			$isgood=intval(odbc_result($rs,'type'))==1?1:0;
			$islock=intval(odbc_result($rs,'type'))==2?1:0;
			$isrecycle=intval(odbc_result($rs,'type'))==3?1:0;

			$topicobj['id']=odbc_result($rs,'id');
			$topicobj['fid']=odbc_result($rs,'bd');
			$topicobj['userid']=odbc_result($rs,'userid');
			$topicobj['username']=addslashes($topicinfo[2]);
			$topicobj['title']=addslashes($topicinfo[1]);
			$topicobj['posttime']=strtotime(odbc_result($rs,'edittime'))-8*3600;
			$topicobj['edittime']=0;
			$topicobj['ordertime']=$topicobj['posttime'];
			$topicobj['lastreply']=time()."\t".$retopic[2];
			$topicobj['replynum']=odbc_result($rs,'renum');
			$topicobj['hits']=odbc_result($rs,'hits');
			$topicobj['istop']=$istop;
			$topicobj['isgood']=$isgood;
			$topicobj['islock']=$islock;
			$topicobj['isalltop']=$isalltop;
			$topicobj['isrecycle']=$isrecycle;
			array_push($topics,$topicobj);
		}
		foreach($topics as $key=>$topic){
			$db->row_insert("topics",$topic);
			$topic['title']=htmlFilter($topic['title']);
			//echo"{$topic['id']}{$topic['title']} 导入成功<br />";
		}
		$page++;
		echo("<script>window.location.href='index.php?step={$step}&page={$page}&epagenum={$epagenum}';</script>");
	break;

	case 4:
		//TODO:
		/*******帖子内容******特殊字符问题 html **/
		$convert_bbs=array("bbs1","bbs2");
		$page==1 && $db->row_delete("posts","id>0");
		//$maxrs=$access->first_array("select max(bbstable.bbsid) as maxid from (select max(bbsid) as bbsid from bbs1 UNION ALL select max(bbsid) as bbsid from bbs2) as bbstable");
		$maxrs=$access->first_array("select max(bbstable.bbb) as maxid from (select max(bbsid) as bbb from bbs1 UNION ALL select max(bbsid) as bbb from bbs2) as bbstable");
		var_dump($maxrs);
		//exit();
		$maxid=intval($maxrs['maxid']);
		if($id_min>$maxid){
			exit("帖子内容数据转换完毕，所有步骤已经转换完毕。<br />请登录论坛后台，重新生成论坛缓存、统计数据整理、设置版块的版主。");
		}
		$bbs1=array();
		$rs=$access->query("select * from (select * from [bbs1] where bbsid BETWEEN {$id_min} AND {$id_max} UNION ALL select * from [bbs2] where bbsid BETWEEN {$id_min} AND {$id_max}) as nnn");
		while($access->fetch_row($rs)){
			$tid=intval(odbc_result($rs,'id'))>0?intval(odbc_result($rs,'id')):intval(odbc_result($rs,'bid'));
			$istopic=intval(odbc_result($rs,'id'))>0?1:0;
			$content=_iconv(odbc_result($rs,'content'));
			$contents=explode("|",$content);
			$content=$contents[4];
			$title=$contents[1];
			$username=$contents[2];
			$bbs1obj['tid']=$tid;
			$bbs1obj['istopic']=$istopic;
			$bbs1obj['fid']=odbc_result($rs,'bd');
			$bbs1obj['userid']=odbc_result($rs,'userid');
			$bbs1obj['username']=addslashes($username);
			$bbs1obj['title']=addslashes($title);
			$bbs1obj['content']=addslashes(html2ubb($content));
			$bbs1obj['posttime']=strtotime(odbc_result($rs,'edittime'))-8*3600;
			$bbs1obj['edittime']=0;
			array_push($bbs1,$bbs1obj);
		}
		foreach($bbs1 as $key=>$bbs){
			$db->row_insert("posts",$bbs);
			//echo"{$bbs['username']}发表的帖子 导入成功<br />";
		}
		$page++;
		echo("<script>window.location.href='index.php?step={$step}&page={$page}&epagenum={$epagenum}';</script>");
	break;

}

function _iconv($str){
	return iconv("gbk", "UTF-8", $str); 
}

function html2ubb($str)
 {
  $str = str_replace("\n",'',$str);
  $str = preg_replace("/\<A[^>]+HREF=\"([^\"]+)\"[^>]*\>(.*?)<\/a\>/i","[url=$1]$2[/url]",$str);
  $str = preg_replace("/\<font(.*?)color=\"#([^ >]+)\"(.*?)\>(.*?)<\/font>/i","<font$1$3>[color=$2]$4[/color]</font>",$str);
  $str = preg_replace("/\<font(.*?)face=\"([^ >]+)\"(.*?)\>(.*?)<\/font>/i","<font$1$3>[face=$2]$4[/face]</font>",$str);
  $str = preg_replace("/\<font(.*?)size=\"([^ >]+)\"(.*?)\>(.*?)<\/font>/i","[size=$2]$4[/size]",$str);
  $str = preg_replace("/\<img[^>]+src=\"([^\"]+)\"[^>]*\>/i","[img]$1[/img]",$str);
  $str = preg_replace("/\<DIV[^>]+ALIGN=\"([^\"]+)\"[^>]*\>(.*?)<\/DIV\>/i","[align=$1]$2[/align]",$str);
  $str = preg_replace("/\<([\/]?)u\>/i","[$1u]",$str);
  $str = preg_replace("/\<([\/]?)em\>/i","[$1I]",$str);
  $str = preg_replace("/\<([\/]?)strong\>/i","[$1b]",$str);
  $str = preg_replace("/\<([\/]?)b(.*?)\>/i","[$1b]",$str);
  $str = preg_replace("/\<([\/]?)i\>/i","[$1i]",$str);
  $str = preg_replace("/<[^>]*?>/i","",$str);
  return $str;
 }
?>
</body>
</html>
